/*
学生信息管理程序　
假设学生成绩信息描述如下：
学号，姓名，性别，民族，年龄，课程名称，成绩
要求程序能够实现：
1）增加、删除、修改学生的基本信息；
2）查询功能：查询学生人数；按性别查询学生人数；
按民族查询学生人数；统计学生的平均分数；
给学生排榜；按课程统计学生分数；
查询获得某课程的最高分的学生信息；
3）存储学生信息：将学生信息按格式存到文件中。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME_LEN 50
#define MAX_COURSE_LEN 50
#define MAX_STUDENTS 100

typedef struct {
	int id; 
	char name[MAX_NAME_LEN]; 
	char gender; 
	char ethnicity[MAX_NAME_LEN]; 
	int age; 
	char course[MAX_COURSE_LEN]; 
	float score; 
} Student;

Student students[MAX_STUDENTS];
int student_count = 0; 

void add_student() {
	if (student_count >= MAX_STUDENTS) {
		printf("学生数量已达到上限，无法继续添加。\n");
		return;
	}
	
	Student new_student;
	
	printf("请输入学号: ");
	scanf("%d", &new_student.id);
	printf("请输入姓名: ");
	scanf("%s", new_student.name);
	printf("请输入性别 (M/F): ");
	scanf(" %c", &new_student.gender);
	printf("请输入民族: ");
	scanf("%s", new_student.ethnicity);
	printf("请输入年龄: ");
	scanf("%d", &new_student.age);
	printf("请输入课程名称: ");
	scanf("%s", new_student.course);
	printf("请输入成绩: ");
	scanf("%f", &new_student.score);
	
	students[student_count++] = new_student;
	printf("学生信息添加成功！\n");
}

void delete_student(int id) {
	int found = 0;
	for (int i = 0; i < student_count; i++) {
		if (students[i].id == id) {
			found = 1;
			for (int j = i; j < student_count - 1; j++) {
				students[j] = students[j + 1];
			}
			student_count--;
			printf("学生信息删除成功！\n");
			break;
		}
	}
	if (!found) {
		printf("未找到学号为 %d 的学生。\n", id);
	}
}

void modify_student(int id) {
	int found = 0;
	for (int i = 0; i < student_count; i++) {
		if (students[i].id == id) {
			found = 1;
			printf("请输入新的姓名: ");
			scanf("%s", students[i].name);
			printf("请输入新的性别 (M/F): ");
			scanf(" %c", &students[i].gender);
			printf("请输入新的民族: ");
			scanf("%s", students[i].ethnicity);
			printf("请输入新的年龄: ");
			scanf("%d", &students[i].age);
			printf("请输入新的课程名称: ");
			scanf("%s", students[i].course);
			printf("请输入新的成绩: ");
			scanf("%f", &students[i].score);
			printf("学生信息修改成功！\n");
			break;
		}
	}
	if (!found) {
		printf("未找到学号为 %d 的学生。\n", id);
	}
}

void query_student_count() {
	printf("当前学生人数: %d\n", student_count);
}

void query_student_count_by_gender(char gender) {
	int count = 0;
	for (int i = 0; i < student_count; i++) {
		if (students[i].gender == gender) {
			count++;
		}
	}
	printf("性别为 %c 的学生人数: %d\n", gender, count);
}

void query_student_count_by_ethnicity(char *ethnicity) {
	int count = 0;
	for (int i = 0; i < student_count; i++) {
		if (strcmp(students[i].ethnicity, ethnicity) == 0) {
			count++;
		}
	}
	printf("民族为 %s 的学生人数: %d\n", ethnicity, count);
}

void calculate_average_score() {
	float total_score = 0;
	for (int i = 0; i < student_count; i++) {
		total_score += students[i].score;
	}
	printf("学生的平均分数: %.2f\n", total_score / student_count);
}

void rank_students() {
	Student temp;
	for (int i = 0; i < student_count - 1; i++) {
		for (int j = i + 1; j < student_count; j++) {
			if (students[i].score < students[j].score) {
				temp = students[i];
				students[i] = students[j];
				students[j] = temp;
			}
		}
	}
	printf("学生排名如下:\n");
	for (int i = 0; i < student_count; i++) {
		printf("%d. %s (%.2f)\n", i + 1, students[i].name, students[i].score);
	}
}

void query_scores_by_course(char *course) {
	printf("课程 %s 的学生分数如下:\n", course);
	for (int i = 0; i < student_count; i++) {
		if (strcmp(students[i].course, course) == 0) {
			printf("%s: %.2f\n", students[i].name, students[i].score);
		}
	}
}

void query_top_student_by_course(char *course) {
	float max_score = -1;
	int top_student_index = -1;
	for (int i = 0; i < student_count; i++) {
		if (strcmp(students[i].course, course) == 0 && students[i].score > max_score) {
			max_score = students[i].score;
			top_student_index = i;
		}
	}
	if (top_student_index != -1) {
		printf("课程 %s 的最高分学生信息:\n", course);
		printf("学号: %d, 姓名: %s, 性别: %c, 民族: %s, 年龄: %d, 成绩: %.2f\n",
			students[top_student_index].id, students[top_student_index].name,
			students[top_student_index].gender, students[top_student_index].ethnicity,
			students[top_student_index].age, students[top_student_index].score);
	} else {
		printf("未找到课程 %s 的学生信息。\n", course);
	}
}

void save_students_to_file(char *filename) {
	FILE *file = fopen(filename, "w");
	if (file == NULL) {
		printf("无法打开文件 %s\n", filename);
		return;
	}
	
	for (int i = 0; i < student_count; i++) {
		fprintf(file, "%d %s %c %s %d %s %.2f\n",
			students[i].id, students[i].name, students[i].gender,
			students[i].ethnicity, students[i].age, students[i].course,
			students[i].score);
	}
	
	fclose(file);
	printf("学生信息已保存到文件 %s\n", filename);
}

int main() {
	int choice;
	int id;
	char gender;
	char ethnicity[MAX_NAME_LEN];
	char course[MAX_COURSE_LEN];
	char filename[MAX_NAME_LEN];
	
	while (1) {
		printf("\n学生信息管理系统\n");
		printf("1. 增加学生信息\n");
		printf("2. 删除学生信息\n");
		printf("3. 修改学生信息\n");
		printf("4. 查询学生人数\n");
		printf("5. 按性别查询学生人数\n");
		printf("6. 按民族查询学生人数\n");
		printf("7. 统计学生的平均分数\n");
		printf("8. 给学生排榜\n");
		printf("9. 按课程统计学生分数\n");
		printf("10. 查询获得某课程的最高分的学生信息\n");
		printf("11. 存储学生信息到文件\n");
		printf("0. 退出\n");
		printf("请选择操作: ");
		scanf("%d", &choice);
		
		switch (choice) {
		case 1:
			add_student();
			break;
		case 2:
			printf("请输入要删除的学生的学号: ");
			scanf("%d", &id);
			delete_student(id);
			break;
		case 3:
			printf("请输入要修改的学生的学号: ");
			scanf("%d", &id);
			modify_student(id);
			break;
		case 4:
			query_student_count();
			break;
		case 5:
			printf("请输入要查询的性别 (M/F): ");
			scanf(" %c", &gender);
			query_student_count_by_gender(gender);
			break;
		case 6:
			printf("请输入要查询的民族: ");
			scanf("%s", ethnicity);
			query_student_count_by_ethnicity(ethnicity);
			break;
		case 7:
			calculate_average_score();
			break;
		case 8:
			rank_students();
			break;
		case 9:
			printf("请输入要查询的课程名称: ");
			scanf("%s", course);
			query_scores_by_course(course);
			break;
		case 10:
			printf("请输入要查询的课程名称: ");
			scanf("%s", course);
			query_top_student_by_course(course);
			break;
		case 11:
			printf("请输入要保存的文件名: ");
			scanf("%s", filename);
			save_students_to_file(filename);
			break;
		case 0:
			printf("退出程序。\n");
			return 0;
		default:
			printf("无效的选择，请重新输入。\n");
		}
	}
	
	return 0;
}

